﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace MishraReader.Entities
{
    public interface IShareServicesProvider
    {
        IEnumerable<IShareServicePackage> AvailableServices { get; }
    }

    public class ShareServicesProvider : IShareServicesProvider
    {
        //public static Guid DeliciousServiceId = new Guid("A96320A7-F3CE-466A-A8B6-496955236511");
        //public static Guid EvernoteServiceId = new Guid("BC745E36-0026-4F11-AF70-CC3945272009");
        //public static Guid FacebookServiceId = new Guid("7D2EA6A9-BE73-41BE-A496-6AEB49C2584F");
        //public static Guid InstaPaperServiceId = new Guid("8EF5DCAD-9FFF-4FDE-BED1-FAE9FB0C5B09");
        //public static Guid OneNoteServiceId = new Guid("49C7037C-B6AA-4591-AA15-FA5C4A4F8A1C");
        //public static Guid ReadItLaterServiceId = new Guid("435F51F8-C02E-440F-941F-1D107C557D86");
        //public static Guid TwitterServiceId = new Guid("D73653C3-1A7C-4171-887B-FA5C81CD1C6F");
        private readonly IShareServicePackage[] _allShareServices;

        private readonly Lazy<IEnumerable<IShareServicePackage>> _availableServices;

        public ShareServicesProvider(IShareServicePackage[] allShareServices)
        {
            _allShareServices = allShareServices;
            _availableServices = new Lazy<IEnumerable<IShareServicePackage>>(GetAvailableServices);
        }

        public IEnumerable<IShareServicePackage> AvailableServices
        {
            get { return _availableServices.Value; }
        }

        private IEnumerable<IShareServicePackage> GetAvailableServices()
        {
            var services = new List<IShareServicePackage>();

            _allShareServices.ForEach(p => p.Initialize());

            services.AddRange(_allShareServices);

            services.Sort(CompareService);

            return services;
        }

        private static int CompareService(IShareServicePackage a, IShareServicePackage b)
        {
            return string.Compare(a.ServiceName, b.ServiceName, StringComparison.CurrentCulture);
        }
    }
}